package com.zr.WarehouseBelonging.service.impl;

import com.zr.WarehouseBelonging.dao.WarehouseBelongingMapper;
import com.zr.WarehouseBelonging.pojo.*;
import com.zr.WarehouseBelonging.service.WarehouseBelongingService;
import com.zr.meiju.StatusEnum;
import com.zr.util.AllRecords;
import com.zr.util.ResultBuildVo;
import com.zr.util.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 11703 on 2019/7/16.
 */
@Service
@Slf4j
public class WarehouseBelongingServiceImpl implements WarehouseBelongingService {

    @Autowired
    private WarehouseBelongingMapper warehouseBelongingMapper;
    @Override
    public Legalperson ByLegalPersonId(Integer id) {
            return warehouseBelongingMapper.ByLegalPersonId(id);

    }

    @Override
    public ResultVo updateState(DepotStatusVo depotStatusVo) {
        Depot queryByDepotCode = warehouseBelongingMapper.queryByDepotCode(depotStatusVo.getDepotCode());
        if(queryByDepotCode==null){
            return ResultBuildVo.error("修改的数据不存在","500");
        }
        Depot depot = new Depot();
        Date date = new Date();
        depot.setUpdateTime(date);
        depot.setUpdateName("小黑土");
        BeanUtils.copyProperties(depotStatusVo, depot);
        warehouseBelongingMapper.updateState(depot);
        return ResultBuildVo.success(depotStatusVo);
    }

    @Override
    public ResultVo<AllRecords> queryPage(DepotSelectVo depotSelectVo) {
        //查询数据
        List<Depot> depotList = warehouseBelongingMapper.queryPage(depotSelectVo);
        for (Depot depot:depotList) {
            Integer status = depot.getStatus()==true?1:0;
            depot.setStatusName(StatusEnum.getName(status));
        }
        //查询数量
        int count = warehouseBelongingMapper.queryCount(depotSelectVo);
        //把当前页,每一页的大小,总页数,总条数,放入到AllRecords
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(depotSelectVo.getPageIndex());
        allRecords.setPageSize(depotSelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        allRecords.setDataList(depotList);
        return ResultBuildVo.success(allRecords);
    }

    @Override
    public ResultVo<List<WarehouseBeelongingSelectVo>> wareSee(WarehouseBeelongingSelectVo warehouseBeelongingSelectVo) {
        //根据法人查看库房中的信息
        Depot depot = warehouseBelongingMapper.queryDepot(warehouseBeelongingSelectVo.getLegalPersonId());
        //根据库房id查看库区中的信息
        Depotarea depotarea = warehouseBelongingMapper.queryDepotarea(warehouseBeelongingSelectVo.getDepotId());
        //根据库区id查看库位中的信息
        List<Depotposition> depotpositionList = warehouseBelongingMapper.queryDepotposition(warehouseBeelongingSelectVo.getDepotIdentifyId());

        WarehouseBeelongingSelectVo warehouseBeelongingSelectVo1 = new WarehouseBeelongingSelectVo();
        BeanUtils.copyProperties(depot,warehouseBeelongingSelectVo1);
        BeanUtils.copyProperties(depotarea,warehouseBeelongingSelectVo1);
        warehouseBeelongingSelectVo1.setDepotpositionList(depotpositionList);
        List<WarehouseBeelongingSelectVo> list = new ArrayList<>();
        list.add(warehouseBeelongingSelectVo1);
        return ResultBuildVo.success(list);
    }

    @Override
    public ResultVo<AllRecords> queryPageDepot(DepotSelectVo depotSelectVo) {
        //查询数据
        List<Depot> depotList = warehouseBelongingMapper.queryPageDepot(depotSelectVo);
        //查询数量
        int count = warehouseBelongingMapper.queryCount(depotSelectVo);
        //把当前页,每一页的大小,总页数,总条数,放入到AllRecords
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(depotSelectVo.getPageIndex());
        allRecords.setPageSize(depotSelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        allRecords.setDataList(depotList);
        return ResultBuildVo.success(allRecords);
    }

    /**
     * 根据库房的id查询库区
     */
    @Override
    public List<Integer> queryDepotById(List<Integer> depotId) {
        return warehouseBelongingMapper.queryDepotById(depotId);
    }

    /**
     * 根据库区的id查询库位
     */
    @Override
    public List<Integer> queryDepotareaById(List<Integer> depotareaId) {
        return warehouseBelongingMapper.queryDepotareaById(depotareaId);
    }

    @Override
    public ResultVo addWarehouseBelonging(WarehouseBelongingAddVo warehouseBelongingAddVo) {
        int legalpersonId = warehouseBelongingAddVo.getLegalpersonId();
        int DepotpositionId = warehouseBelongingAddVo.getDepotpositionId();
        //进行逻辑验证
        ResultVo validation = validation(warehouseBelongingAddVo);
        if(!validation.isSuccess()){
                return validation;
        }
        return warehouseBelongingMapper.addWarehouseBelonging(legalpersonId,DepotpositionId);
    }

    /**
     *
     * 新增时的逻辑验证
     * @param warehouseBelongingAddVo
     * @return
     */
    public ResultVo validation(WarehouseBelongingAddVo warehouseBelongingAddVo) {
        if (!StringUtils.isEmpty(warehouseBelongingAddVo.getLegalpersonId())) {
            Legalperson legalperson = warehouseBelongingMapper.queryLegalPerson(warehouseBelongingAddVo.getLegalpersonId());
            if (StringUtils.isEmpty(legalperson)) {
                return ResultBuildVo.error("新增时法人不可以为空", "500");
            }
        } else {
            return ResultBuildVo.error("法人的di不可以为空", "500");
        }
        return ResultBuildVo.success(warehouseBelongingAddVo);
    }
}
